/*初始化函数*/
function init() {
    let layer = layui.layer, form = layui.form;

    // 0.1 加载分类
    $.get("/typeInfo", {"token": localStorage.getItem("token")}, function (result) {
        if (result.code === 200) {
            let html_str = '<option value="">请选择</option>';
            $.each(result.data, function (i, n) {
                html_str += '<option value="' + n.id + '">' + n.name + '</option>';
            })
            $("select[name='search_type_id']").html(html_str);
            form.render("select");//渲染select框
        } else if (result.code === 401) window.parent.location.href = "/login.html";
        else layer.msg(result.msg, {icon: 5});
        layer.closeAll("loading");
    })

    // 0.2 加载标签
    $.get("/tagInfo", {"token": localStorage.getItem("token")}, function (result) {
        if (result.code === 200) {
            let html_str = "";
            $.each(result.data, function (i, n) {
                html_str += '<input type="checkbox" name="search_tag" title="' + n.name + '" value="' + n.id + '">';
            })
            $("#search_tag_div").html(html_str);
            form.render("checkbox");//渲染checkbox

            // 0.3 点击查询按钮
            $("input[name='search_title']").val("");
            $("input[name='search_username']").val("");
            $("select[name='search_type_id']").val("");
            $("#search_btn").click();
        } else if (result.code === 401) window.parent.location.href = "/login.html";
        else layer.msg(result.msg, {icon: 5});
    })

}

layui.use(['layer', 'form'], function () {
    let layer = layui.layer, form = layui.form;

    // 1.查询按钮
    $("#search_btn").click(function () {
        layer.load(1);
        let title = $.trim($("input[name='search_title']").val());
        let username = $.trim($("input[name='search_username']").val());
        let type_id = $("select[name='search_type_id']").val();
        let $tag_checked = $("input[type='checkbox'][name='search_tag']:checked");
        let tag = [];
        $.each($tag_checked, function (i, n) {
            tag[i] = n.value;
        })
        let published =false;//草稿箱为false
        $.ajax({
            url: "/blog/info",
            method: "GET",
            data: {
                "token": localStorage.getItem("token"),
                "title": title,
                "username": username,
                "type_id": type_id,
                "tag": tag,
                "published": published
            },
            dataType: "JSON",
            traditional: true,//开启数组参数传递
            success: function (result) {
                if (result.code === 200) {
                    let html_str = "";
                    $.each(result.data, function (i, n) {
                        html_str += '<tr><td><input type="radio" name="blog_id" value="' + n.id + '"></td>';
                        html_str += '<td>' + n.title + '</td>';
                        html_str += '<td>' + n.createUser.username + '</td>';
                        html_str += '<td>' + n.typeName + '</td>';
                        html_str += '<td>'
                        $.each(n.tagList, function (j, m) {
                            html_str += '<div class="layui-row">';
                            html_str += '<div class="layui-col-xs12">';
                            html_str += '<button class="layui-btn layui-btn-primary layui-btn-xs layui-btn-radius layui-font-blue">' + m.name + '</button>';
                            html_str += '</div></div>';
                        })
                        html_str += '</td>';
                        html_str += '<td>' + n.recommendLevel + '</td>';
                        html_str += '<td>' + n.createFlag + '</td>';
                        html_str += '<td>' + n.published + '</td></tr>';
                    })
                    $("#blog_list").html(html_str);
                } else layer.msg(result.msg, {icon: 5});
                layer.closeAll("loading");
            }
        })
    })

    // 2.修改按钮
    $("#edit_btn").click(function () {
        let $blog_checked = $("input[type='radio'][name='blog_id']:checked");
        let published = false;//草稿箱为false
        if ($blog_checked.length !== 1) {
            layer.msg("请选择需要修改的博客", {icon: 5});
            return false;
        }
        layer.confirm("将跳转页面进行修改", {icon: 3, title: "提示"}, function (index) {
            layer.close(index);
            window.parent.$("#blog_id").val($blog_checked.val());//将需要修改的博客id放入
            window.parent.$("#blog_edit_flag").val(true);// 将修改标记打开
            window.parent.$("#blog_published").val(published);// 将博客是否发布的值也传入
            window.location.href = "/settings/add_blog.html";
        })
    })

    // 3.删除按钮
    $("#delete_btn").click(function () {
        let $blog_checked = $("input[type='radio'][name='blog_id']:checked");
        if ($blog_checked.length !== 1) {
            layer.msg("请选择需要删除的博客", {icon: 5});
            return false;
        }
        layer.confirm("确认要删除博客：" + $blog_checked.parent().next().html(),
            {icon: 3, title: "警告"}, function (index) {
                $.ajax({
                    url: "/blog",
                    method: "delete",
                    data: {
                        "token": localStorage.getItem("token"),
                        "id": $blog_checked.val()
                    }, dataType: "json",
                    success: function (result) {
                        if (result.code === 200) {
                            $blog_checked.parent().parent().remove();
                            layer.close(index);
                            layer.msg(result.msg, {icon: 1});
                        }else layer.msg(result.msg);
                    }
                })
            })
    })

    // 开始初始化
    init();
})
